//
//  HeadImageViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/10.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//

#import "HeadImageViewController.h"

@interface HeadImageViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation HeadImageViewController
{
    UIImage *_selectedImg;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageStr] placeholderImage:[UIImage imageNamed:@"占位图"]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStyleDone target:self action:@selector(moreAction:)];
}
- (void)moreAction:(UIBarButtonItem *)bar
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
        pickerController.view.backgroundColor = [UIColor orangeColor];
        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
        pickerController.sourceType = sourcheType;
        pickerController.delegate = self;
        pickerController.allowsEditing = YES;
        [self presentViewController:pickerController animated:YES completion:nil];
    }];
    [action1 setValue:[UIColor colorWithRed:105.0/255.0 green:112.0/255.0 blue:111.0/255.0 alpha:1] forKey:@"titleTextColor"];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
        pickerController.view.backgroundColor = [UIColor orangeColor];
        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        pickerController.sourceType = sourcheType;
        pickerController.delegate = self;
        pickerController.allowsEditing = YES;
        [self presentViewController:pickerController animated:YES completion:nil];
    }];
    [action2 setValue:[UIColor colorWithRed:105.0/255.0 green:112.0/255.0 blue:111.0/255.0 alpha:1] forKey:@"titleTextColor"];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }];
    [action3 setValue:[UIColor colorWithRed:105.0/255.0 green:112.0/255.0 blue:111.0/255.0 alpha:1] forKey:@"titleTextColor"];
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [action4 setValue:[UIColor colorWithRed:105.0/255.0 green:112.0/255.0 blue:111.0/255.0 alpha:1] forKey:@"titleTextColor"];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [alert addAction:action4];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark ------------ 保存图片成功失败
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error == nil) {
        
        [[SQShowMessage shareMessage] showMessageWithStr:@"保存成功"];
        
    }else{
        
        [[SQShowMessage shareMessage] showMessageWithStr:@"保存失败"];
    }
    
}
#pragma mark ------------ 当用户选取完成后调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSString *b = [self getDataLine];
    NSString *tokenStr = [PMQiNiuToken generateUploadTokenWithScopeName:[NSString stringWithFormat:@"weiyiyunshang:%@%@.jpg",UID,b]];
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    NSData *data = UIImageJPEGRepresentation(info[UIImagePickerControllerEditedImage], 0.1);
    _selectedImg = info[UIImagePickerControllerEditedImage];
    [upManager putData:data key:[NSString stringWithFormat:@"%@%@.jpg",UID,b] token:tokenStr complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        NSString *imgUrl = [NSString stringWithFormat:@"http://image.vmijob.com/%@",resp[@"key"]];
        [NetRequiest handleDataWithString:HTTPLOAD dictionary:@{@"v1":@"reviewmaterial",@"uid":UID,@"img_url":imgUrl} HUD:YES block:^(id responseObject) {
            if ([responseObject[@"code"] isEqual:@200]) {
                self.imageView.image = _selectedImg;
            }
            [picker dismissViewControllerAnimated:YES completion:nil];
            [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
        }];
    } option:nil];
}
#pragma mark ------------ 当用户取消选取时调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSString *)getDataLine
{
    NSDate *date = [NSDate date];
    NSTimeInterval timeIn = [date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%f",timeIn];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
